﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class UIItemTransformUnitComponent : MonoBehaviour {
    public static UIItemTransformUnitComponent lookNow;


    /// <summary>
    /// 目标
    /// </summary>
    public Transform unit;

    /// <summary>
    /// 选中时高亮的 spriteBG
    /// </summary>
    [SerializeField]
    protected GameObject spriteBG;

    [SerializeField]
    protected UIInput inputName;

    protected string _name;
    public string Name
    {
        get
        {
            return _name;
        }

        set
        {
            inputName.value = value;
            _name = value;
            name = value;
        }
    }

    [SerializeField]
    protected UIInput inputPositionX;
    [SerializeField]
    protected UIInput inputPositionY;
    [SerializeField]
    protected UIInput inputPositionZ;
    Vector3 position;
    public Vector3 Position
    {
        get
        {
            return position;
        }

        set
        {
            inputPositionX.value = value.x.ToString();
            inputPositionY.value = value.y.ToString();
            inputPositionZ.value = value.z.ToString();
            position = value;
        }
    }
    public void SetPositionX(float value)
    {
        inputPositionX.value = value.ToString();
        position.x = value;
    }
    public void SetPositionY(float value)
    {
        inputPositionY.value = value.ToString();
        position.y = value;
    }
    public void SetPositionZ(float value)
    {
        inputPositionZ.value = value.ToString();
        position.z = value;
    }



    protected void UIChangPositionX()
    {
        float x;
        if (float.TryParse(inputPositionX.value, out x))
        {
            position.x = x;
            TransformVector.Thas.transform.position = position;
        }
    }

    protected void UIChangPositionY()
    {
        float y;
        if (float.TryParse(inputPositionY.value, out y))
        {
            position.y = y;
            TransformVector.Thas.transform.position = position;
        }
    }

    protected void UIChangPositionZ()
    {
        float z;
        if (float.TryParse(inputPositionZ.value, out z))
        {
            position.z = z;
            TransformVector.Thas.transform.position = position;
        }
    }

    public void Show(string name, Vector3 position, Transform unit)
    {
        Position = position;
        Name = name;
        this.unit = unit;
        unit.localPosition = position;
        EventDelegate.Add(inputName.onChange, UIChangName);
        EventDelegate.Add(inputPositionX.onChange, UIChangPositionX);
        EventDelegate.Add(inputPositionY.onChange, UIChangPositionY);
        EventDelegate.Add(inputPositionZ.onChange, UIChangPositionZ);
        EventDelegate.Add(this.GetComponent<UIButton>().onClick, UILookThis);
    }

    private void UIChangName()
    {
        this._name = inputName.value;
    }

    public virtual void LookThis()
    {
        if (lookNow)
        {
            lookNow.spriteBG.SetActive(false);
        }
        spriteBG.SetActive(true);
        lookNow = this;
        UIComponentMastermind.instance.ShowLeftUITransform();
    }
    public virtual void UILookThis()
    {
        TransformVector.Thas.Look = unit;
        if (lookNow)
        {
            lookNow.spriteBG.SetActive(false);
        }
        spriteBG.SetActive(true);
        lookNow = this;
        UIComponentMastermind.instance.ShowLeftUITransform();
    }

    public virtual void NoLookThis()
    {
        if (lookNow)
        {
            lookNow.spriteBG.SetActive(false);
            lookNow = null;
        }
    }

    public static void UnLookThis()
    {
        if (lookNow)
        {
            lookNow.NoLookThis();
        }
    }

    public virtual void Delete()
    {
        if (TransformVector.Thas.Look == unit)
        {
            TransformVector.Thas.Look = null;
        }
        if (lookNow == this)
        {
            lookNow = null;
        }
        if (unit)
        {
            Destroy(unit.gameObject);
        }
        Destroy(this.gameObject);
        UINodeTransformComponent.thas.Reposition();
    }

}

